package net.guoyixian.admin.service;

import net.guoyixian.admin.mapper.PermissionsMapper;
import net.guoyixian.admin.pojo.Permissions;
import net.guoyixian.admin.vo.PermissionsVo;
import net.guoyixian.common.datasource.DataSource;
import net.guoyixian.common.datasource.DataSource.SourceType;
import net.guoyixian.common.web.TreeNode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户service
 * 
 */
@Service
public class PermissionsService {
	@Autowired
	private PermissionsMapper permissionsMapper;

	@DataSource(SourceType.SLAVE)
	public List<PermissionsVo> queryTreegrid(Integer id){
		List<Permissions> list;
		if (id != null) {
			list = permissionsMapper.queryByPid(id);
		} else {
			list = permissionsMapper.queryTreegridNull();
		}
		
		return changeModel(list);
	}
	
	@DataSource(SourceType.SLAVE)
	private List<PermissionsVo> changeModel(List<Permissions> list) {
		List<PermissionsVo> l = new ArrayList<PermissionsVo>();
		if (list != null && list.size() > 0) {
			for (Permissions p : list) {
				PermissionsVo a = new PermissionsVo();
				BeanUtils.copyProperties(p, a);
				Permissions parent = new Permissions();
				parent = permissionsMapper.queryById(p.getParentId());
				if (p.getParentId() != null) {
					a.setPid(parent.getId());
					a.setPname(parent.getName());
				}
				if (countChildren(p.getId()) > 0) {
					a.setState("closed");
				}
				l.add(a);
			}
		}
		return l;
	}

	/**
	 * 统计有多少子节点
	 */
	@DataSource(SourceType.SLAVE)
	private Integer countChildren(Integer pid) {
		return permissionsMapper.countChildren(pid);
	}
	/**
	 * 权限树
	 * @param pid
	 * @param b
	 * @return
	 */
	@DataSource(SourceType.SLAVE)
	public List<TreeNode> tree(Integer pid,boolean b){
		List<Permissions> list;
		if (pid != null) {
			list = permissionsMapper.queryByPid(pid);
		}else{
			list = permissionsMapper.queryTreegridNull();
		}
		List<TreeNode> tree = new ArrayList<TreeNode>();
		for (Permissions p : list) {
			tree.add(this.tree(p, b));
		}
		return tree;
	}
	
	@DataSource(SourceType.SLAVE)
	private TreeNode tree(Permissions p,boolean recursive) {
		TreeNode node = new TreeNode();
		node.setId(p.getId());
		node.setText(p.getName());
		Map<String, Object> attributes = new HashMap<String, Object>();
		node.setAttributes(attributes);
		/*查找子节点*/
		List<Permissions> list = permissionsMapper.queryByPid(p.getId());
		if (list.size() > 0) {
			node.setState("closed");
			if (recursive) {// 递归查询子节点
				//Collections.sort(l, new AuthComparator());// 排序
				List<TreeNode> children = new ArrayList<TreeNode>();
				for (Permissions item : list) {
					TreeNode tn = tree(item, true);
					children.add(tn);
				}
				node.setChildren(children);
				node.setState("open");
			}
		}
		return node;
	}

	public void save(Permissions permissions) {
		if (permissions.getId()==null) {
			permissionsMapper.save(permissions);
		}else{
			permissionsMapper.update(permissions);
		}
	}

	public void delete(Integer id) {
		permissionsMapper.delete(id);
	}

}